home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / util / moni / Scout-src.lha / source / objects / scout_audiomode.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-09-17  |  14.3 KB  |  397 lines

  1. /**
  2.  * Scout - The Amiga System Monitor
  3.  *
  4.  *------------------------------------------------------------------
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  * You must not use this source code to gain profit of any kind!
  21.  *
  22.  *------------------------------------------------------------------
  23.  *
  24.  * @author Andreas Gelhausen
  25.  * @author Richard Körber <rkoerber@gmx.de>
  26.  */
  27.  
  28. #include "system_headers.h"
  29.  
  30. struct AudioModesCallbackUserData {
  31.     APTR ud_List;
  32.     ULONG ud_Count;
  33. };
  34.  
  35. static __asm __saveds LONG amodelist_con2func(register __a2 Object *obj, register __a1 struct NList_ConstructMessage *msg, register __a0 struct Hook *hook)
  36. {
  37.     return AllocListEntry(msg->pool, msg->entry, sizeof(struct AudioModeEntry));
  38. }
  39.  
  40. MakeHook(amodelist_con2hook, amodelist_con2func);
  41.  
  42. static __asm __saveds LONG amodelist_des2func(register __a2 Object *obj, register __a1 struct NList_DestructMessage *msg, register __a0 struct Hook *hook)
  43. {
  44.     FreeListEntry(msg->pool, &msg->entry);
  45.  
  46.     return 0;
  47. }
  48.  
  49. MakeHook(amodelist_des2hook, amodelist_des2func);
  50.  
  51. static __asm __saveds LONG amodelist_dsp2func(register __a2 Object *obj, register __a1 struct NList_DisplayMessage *msg, register __a0 struct Hook *hook)
  52. {
  53.     struct AudioModeEntry *ame = (struct AudioModeEntry *)msg->entry;
  54.  
  55.     if (ame) {
  56.         msg->strings[0] = ame->ame_Id;
  57.         msg->strings[1] = ame->ame_Name;
  58.         msg->strings[2] = ame->ame_Bits;
  59.         msg->strings[3] = ame->ame_MinFrequency;
  60.         msg->strings[4] = ame->ame_MaxFrequency;
  61.     } else {
  62.         msg->strings[0] = "ModeID";
  63.         msg->strings[1] = "Name";
  64.         msg->strings[2] = "Bits";
  65.         msg->strings[3] = "min. Freq";
  66.         msg->strings[4] = "max. Freq";
  67.         msg->preparses[0] = MUIX_B;
  68.         msg->preparses[1] = MUIX_B;
  69.         msg->preparses[2] = MUIX_B;
  70.         msg->preparses[3] = MUIX_B;
  71.         msg->preparses[4] = MUIX_B;
  72.     }
  73.  
  74.     return 0;
  75. }
  76.  
  77. MakeHook(amodelist_dsp2hook, amodelist_dsp2func);
  78.  
  79. static LONG amodelist_cmp2colfunc( struct AudioModeEntry *ame1,
  80.                                    struct AudioModeEntry *ame2,
  81.                                    ULONG column )
  82. {
  83.     LONG dec1, dec2;
  84.  
  85.     switch (column) {
  86.         case 0: return stricmp(ame1->ame_Id, ame2->ame_Id);
  87.         case 1: return stricmp(ame1->ame_Name, ame2->ame_Name);
  88.         case 2: IsDec(ame1->ame_Bits, &dec1); IsDec(ame2->ame_Bits, &dec2); return dec2 - dec1;
  89.         case 3: IsDec(ame1->ame_MinFrequency, &dec1); IsDec(ame2->ame_MinFrequency, &dec2); return dec2 - dec1;
  90.         case 4: IsDec(ame1->ame_MaxFrequency, &dec1); IsDec(ame2->ame_MaxFrequency, &dec2); return dec2 - dec1;
  91.     }
  92. }
  93.  
  94. static __asm __saveds LONG amodelist_cmp2func(register __a2 Object *obj, register __a1 struct NList_CompareMessage *msg, register __a0 struct Hook *hook)
  95. {
  96.     LONG cmp;
  97.     struct AudioModeEntry *ame1, *ame2;
  98.     ULONG col1, col2;
  99.  
  100.     ame1 = (struct AudioModeEntry *)msg->entry1;
  101.     ame2 = (struct AudioModeEntry *)msg->entry2;
  102.     col1 = msg->sort_type & MUIV_NList_TitleMark_ColMask;
  103.     col2 = msg->sort_type2 & MUIV_NList_TitleMark2_ColMask;
  104.  
  105.     if (msg->sort_type == MUIV_NList_SortType_None) return 0;
  106.  
  107.     if (msg->sort_type & MUIV_NList_TitleMark_TypeMask) {
  108.         cmp = amodelist_cmp2colfunc(ame2, ame1, col1);
  109.     } else {
  110.         cmp = amodelist_cmp2colfunc(ame1, ame2, col1);
  111.     }
  112.  
  113.     if (cmp != 0 || col1 == col2) return cmp;
  114.  
  115.     if (msg->sort_type2 & MUIV_NList_TitleMark2_TypeMask) {
  116.         cmp = amodelist_cmp2colfunc(ame2, ame1, col2);
  117.     } else {
  118.         cmp = amodelist_cmp2colfunc(ame1, ame2, col2);
  119.     }
  120.  
  121.     return cmp;
  122. }
  123.  
  124. MakeHook(amodelist_cmp2hook, amodelist_cmp2func);
  125.  
  126. static void ReceiveList( void (* callback)( struct AudioModeEntry *ame, void *userData ),
  127.                          void *userData )
  128. {
  129.     struct AudioModeEntry *ame;
  130.  
  131.     if (ame = tbAllocVecPooled(globalPool, sizeof(struct AudioModeEntry))) {
  132.         if (SendDaemon("GetAudioModelist")) {
  133.             while (ReceiveDecodedEntry((UBYTE *)ame, sizeof(struct AudioModeEntry))) {
  134.                 callback(ame, userData);
  135.             }
  136.         }
  137.  
  138.         tbFreeVecPooled(globalPool, ame);
  139.     }
  140. }
  141.  
  142. static void IterateList( void (* callback)( struct AudioModeEntry *ame, void *userData ),
  143.                          void *userData )
  144. {
  145.     struct AudioModeEntry *ame;
  146.  
  147.     if (ame = tbAllocVecPooled(globalPool, sizeof(struct AudioModeEntry))) {
  148.         struct MsgPort *mp;
  149.  
  150.         if (mp = CreateMsgPort()) {
  151.             struct AHIRequest *ahir;
  152.  
  153.             if (ahir = CreateIORequest(mp, sizeof(struct AHIRequest))) {
  154.                 ahir->ahir_Version = 4;
  155.  
  156.                 if (OpenDevice(AHINAME, 0, ahir, 0) == 0) {
  157.                     struct Device *AHIBase;
  158.                     ULONG id = AHI_INVALID_ID;
  159.  
  160.                     AHIBase = ahir->ahir_Std.io_Device;
  161.  
  162.                     while ((id = AHI_NextAudioID(id)) != AHI_INVALID_ID) {
  163.                         ULONG bits, minfreq, maxfreq, numfreqs;
  164.  
  165.                         ame->ame_ModeID = id;
  166.                         _snprintf(ame->ame_Id, sizeof(ame->ame_Id), "$%08lx", id);
  167.  
  168.                         AHI_GetAudioAttrs(id, NULL, AHIDB_Name, ame->ame_Name,
  169.                                                     AHIDB_BufferLen, sizeof(ame->ame_Name),
  170.                                                     TAG_DONE);
  171.                         AHI_GetAudioAttrs(id, NULL, AHIDB_Bits, &bits,
  172.                                                     AHIDB_MinMixFreq, &minfreq,
  173.                                                     AHIDB_MaxMixFreq, &maxfreq,
  174.                                                     AHIDB_Frequencies, &numfreqs,
  175.                                                     TAG_DONE);
  176.                         _snprintf(ame->ame_Bits, sizeof(ame->ame_Bits), "%3lD", bits);
  177.                         if (numfreqs > 1) {
  178.                             _snprintf(ame->ame_MinFrequency, sizeof(ame->ame_MinFrequency), "%6lD Hz", minfreq);
  179.                             _snprintf(ame->ame_MaxFrequency, sizeof(ame->ame_MaxFrequency), "%6lD Hz", maxfreq);
  180.                         } else {
  181.                             stccpy(ame->ame_MinFrequency, "---", sizeof(ame->ame_MinFrequency));
  182.                             stccpy(ame->ame_MaxFrequency, "---", sizeof(ame->ame_MaxFrequency));
  183.                         }
  184.  
  185.                         callback(ame, userData);
  186.                     }
  187.  
  188.                     CloseDevice(ahir);
  189.                 } else {
  190.                     MyRequest(msgErrorContinue, msgCantOpenAHIDevice, AHINAME, ahir->ahir_Version);
  191.                 }
  192.  
  193.                 DeleteIORequest(ahir);
  194.             }
  195.  
  196.             DeleteMsgPort(mp);
  197.         }
  198.  
  199.         tbFreeVecPooled(globalPool, ame);
  200.     }
  201. }
  202.  
  203. static void UpdateCallback( struct AudioModeEntry *ame,
  204.                             void *userData )
  205. {
  206.     struct AudioModesCallbackUserData *ud = (struct AudioModesCallbackUserData *)userData;
  207.  
  208.     InsertSortedEntry(ud->ud_List, ame);
  209.     ud->ud_Count++;
  210. }
  211.  
  212. static void PrintCallback( struct AudioModeEntry *ame,
  213.                            void *userData )
  214. {
  215.     PrintFOneLine((BPTR)userData, " %s %4s %8s %8s %s\n", ame->ame_Id, ame->ame_Bits, ame->ame_MinFrequency, ame->ame_MaxFrequency, ame->ame_Name);
  216. }
  217.  
  218. static void SendCallback( struct AudioModeEntry *ame,
  219.                           void *userData )
  220. {
  221.     SendEncodedEntry((UBYTE *)ame, sizeof(struct AudioModeEntry));
  222. }
  223.  
  224. static ULONG __saveds mNew( struct IClass *cl,
  225.                             Object *obj,
  226.                             struct opSet *msg )
  227. {
  228.     APTR amodelist, amodetext, amodecount, updateButton, printButton, moreButton, exitButton;
  229.  
  230.     if (obj = (Object *)DoSuperNew(cl, obj,
  231.         MUIA_HelpNode, AudioModesText,
  232.         MUIA_Window_ID, MakeID('A','M','D','E'),
  233.         WindowContents, VGroup,
  234.  
  235.             Child, amodelist = MyNListviewObject(MakeID('A','M','L','V'), "BAR,BAR,BAR P=" MUIX_R ",BAR P=" MUIX_R ",BAR P=" MUIX_R, &amodelist_con2hook, &amodelist_des2hook, &amodelist_dsp2hook, &amodelist_cmp2hook, TRUE),
  236.             Child, MyBelowListview(&amodetext, &amodecount),
  237.  
  238.             Child, MyVSpace(4),
  239.  
  240.             Child, HGroup, MUIA_Group_SameSize, TRUE,
  241.                 Child, updateButton = MakeButton(txtUpdate),
  242.                 Child, printButton  = MakeButton(txtPrint),
  243.                 Child, moreButton   = MakeButton(txtMore),
  244.                 Child, exitButton   = MakeButton(txtExit),
  245.             End,
  246.         End,
  247.         TAG_MORE, msg->ops_AttrList))
  248.     {
  249.         struct AudioModesWinData *amwd = INST_DATA(cl, obj);
  250.         APTR parent;
  251.  
  252.         amwd->amwd_AudioModeList = amodelist;
  253.         amwd->amwd_AudioModeText = amodetext;
  254.         amwd->amwd_AudioModeCount = amodecount;
  255.         amwd->amwd_MoreButton = moreButton;
  256.  
  257.         parent = (APTR)GetTagData(MUIA_Window_ParentWindow, (ULONG)NULL, msg->ops_AttrList);
  258.  
  259.         set(obj, MUIA_Window_Title, MyGetWindowTitle("AUDIOMODES", amwd->amwd_Title, sizeof(amwd->amwd_Title)));
  260.         set(obj, MUIA_Window_ActiveObject, amodelist);
  261.         set(moreButton, MUIA_Disabled, TRUE);
  262.  
  263.         DoMethod(parent,       MUIM_Window_AddChildWindow, obj);
  264.         DoMethod(obj,          MUIM_Notify, MUIA_Window_CloseRequest, TRUE,           MUIV_Notify_Application, 5, MUIM_Application_PushMethod, parent, 2, MUIM_Window_RemChildWindow, obj);
  265.         DoMethod(amodelist,    MUIM_Notify, MUIA_NList_Active,        MUIV_EveryTime, obj,                     1, MUIM_AudioModesWin_ListChange);
  266.         DoMethod(amodelist,    MUIM_Notify, MUIA_NList_DoubleClick,   MUIV_EveryTime, obj,                     1, MUIM_AudioModesWin_More);
  267.         DoMethod(updateButton, MUIM_Notify, MUIA_Pressed,             FALSE,          obj,                     1, MUIM_AudioModesWin_Update);
  268.         DoMethod(printButton,  MUIM_Notify, MUIA_Pressed,             FALSE,          obj,                     1, MUIM_AudioModesWin_Print);
  269.         DoMethod(moreButton,   MUIM_Notify, MUIA_Pressed,             FALSE,          obj,                     1, MUIM_AudioModesWin_More);
  270.         DoMethod(exitButton,   MUIM_Notify, MUIA_Pressed,             FALSE,          obj,                     3, MUIM_Set, MUIA_Window_CloseRequest, TRUE);
  271.         DoMethod(amodelist,    MUIM_NList_Sort3, MUIV_NList_Sort3_SortType_1, MUIV_NList_SortTypeAdd_None, MUIV_NList_Sort3_SortType_Both);
  272.     }
  273.  
  274.     return (ULONG)obj;
  275. }
  276.  
  277. static ULONG __saveds mDispose( struct IClass *cl,
  278.                                 Object *obj,
  279.                                 struct opSet *msg )
  280. {
  281.     struct AudioModesWinData *amwd = INST_DATA(cl, obj);
  282.  
  283.     set(obj, MUIA_Window_Open, FALSE);
  284.     DoMethod(amwd->amwd_AudioModeList, MUIM_NList_Clear);
  285.  
  286.     return (DoSuperMethodA(cl, obj, msg));
  287. }
  288.  
  289. static ULONG __saveds mUpdate( struct IClass *cl,
  290.                                Object *obj,
  291.                                Msg msg )
  292. {
  293.     struct AudioModesWinData *amwd = INST_DATA(cl, obj);
  294.     struct AudioModesCallbackUserData ud;
  295.  
  296.     ApplicationSleep(TRUE);
  297.     set(amwd->amwd_AudioModeList, MUIA_NList_Quiet, TRUE);
  298.     DoMethod(amwd->amwd_AudioModeList, MUIM_NList_Clear);
  299.  
  300.     ud.ud_List = amwd->amwd_AudioModeList;
  301.     ud.ud_Count = 0;
  302.  
  303.     if (clientstate) {
  304.         ReceiveList(UpdateCallback, &ud);
  305.     } else {
  306.         IterateList(UpdateCallback, &ud);
  307.     }
  308.  
  309.     SetCountText(amwd->amwd_AudioModeCount, ud.ud_Count);
  310.     MySetContents(amwd->amwd_AudioModeText, "");
  311.  
  312.     set(amwd->amwd_AudioModeList, MUIA_NList_Quiet, FALSE);
  313.     set(amwd->amwd_MoreButton, MUIA_Disabled, TRUE);
  314.     ApplicationSleep(FALSE);
  315.  
  316.     return 0;
  317. }
  318.  
  319. static ULONG __saveds mPrint( struct IClass *cl,
  320.                               Object *obj,
  321.                               Msg msg )
  322. {
  323.     PrintAudioModes(NULL);
  324.  
  325.     return 0;
  326. }
  327.  
  328. static ULONG __saveds mMore( struct IClass *cl,
  329.                              Object *obj,
  330.                              Msg msg )
  331. {
  332.     struct AudioModesWinData *amwd = INST_DATA(cl, obj);
  333.     struct AudioModeEntry *ame;
  334.  
  335.     if (ame = (struct AudioModeEntry *)GetActiveEntry(amwd->amwd_AudioModeList)) {
  336.         APTR detailWin;
  337.  
  338.         if (detailWin = AudioModesDetailWindowObject,
  339.                 MUIA_Window_ParentWindow, obj,
  340.             End) {
  341.             set(detailWin, MUIA_AudioModesDetailWin_AudioMode, ame);
  342.             set(detailWin, MUIA_Window_Open, TRUE);
  343.         }
  344.     }
  345.  
  346.     return 0;
  347. }
  348.  
  349. static ULONG __saveds mListChange( struct IClass *cl,
  350.                                    Object *obj,
  351.                                    Msg msg )
  352. {
  353.     struct AudioModesWinData *amwd = INST_DATA(cl, obj);
  354.     struct AudioModeEntry *ame;
  355.  
  356.     if (ame = (struct AudioModeEntry *)GetActiveEntry(amwd->amwd_AudioModeList)) {
  357.         MySetContents(amwd->amwd_AudioModeText, "%s \"%s\"", ame->ame_Id, ame->ame_Name);
  358.         if (!clientstate) set(amwd->amwd_MoreButton, MUIA_Disabled, FALSE);
  359.     }
  360.  
  361.     return 0;
  362. }
  363.  
  364. ULONG __asm __saveds AudioModesWinDispatcher( register __a0 struct IClass *cl,
  365.                                               register __a2 Object *obj,
  366.                                               register __a1 Msg msg )
  367. {
  368.     switch (msg->MethodID) {
  369.         case OM_NEW:                        return (mNew(cl, obj, (APTR)msg));
  370.         case OM_DISPOSE:                    return (mDispose(cl, obj, (APTR)msg));
  371.         case MUIM_AudioModesWin_Update:     return (mUpdate(cl, obj, (APTR)msg));
  372.         case MUIM_AudioModesWin_Print:      return (mPrint(cl, obj, (APTR)msg));
  373.         case MUIM_AudioModesWin_More:       return (mMore(cl, obj, (APTR)msg));
  374.         case MUIM_AudioModesWin_ListChange: return (mListChange(cl, obj, (APTR)msg));
  375.     }
  376.  
  377.     return (DoSuperMethodA(cl, obj, msg));
  378. }
  379.  
  380. void PrintAudioModes( char *filename )
  381. {
  382.     BPTR handle;
  383.  
  384.     if (handle = HandlePrintStart(filename)) {
  385.         PrintFOneLine(handle, "\n  ModeID    Bits MinFreq  MaxFreq Name\n\n");
  386.         IterateList(PrintCallback, (void *)handle);
  387.     }
  388.  
  389.     HandlePrintStop();
  390. }
  391.  
  392. void SendAudioModeList( void )
  393. {
  394.     IterateList(SendCallback, NULL);
  395. }
  396.  
  397.